package com.example.sqldealtool.translate;

import com.google.gson.Gson;

import java.io.*;
import java.util.List;
import java.util.Map;

/**
 * @author: xjc
 * @date: 2023/4/10 14:40
 **/
public class Translation {
    // 在平台申请的APP_ID 详见 http://api.fanyi.baidu.com/api/trans/product/desktop?req=developer
    private static final String APP_ID = "APP_ID";
    private static final String SECURITY_KEY = "SECURITY_KEY";

    public static void main(String[] args) {
        String query = "高度600米";
        System.out.println(zhToEn(query));
    }
    public static String zhToEn(String text){
        TransApi api = new TransApi(APP_ID, SECURITY_KEY);
        Gson gson = new Gson();
        String result = api.getTransResult(text, "zh", "en");
        Map<String,Object> resultMap = gson.fromJson(result,Map.class);
        List<Map<String,String>> trans_result = (List<Map<String, String>>) resultMap.get("trans_result");
        Map<String,String> trans_resultMap = trans_result.get(0);
        return trans_resultMap.get("dst");
    }

    public static String enToZh(String text){
        TransApi api = new TransApi(APP_ID, SECURITY_KEY);
        Gson gson = new Gson();
        String result = api.getTransResult(text, "en", "zh");
        Map<String,Object> resultMap = gson.fromJson(result,Map.class);
        List<Map<String,String>> trans_result = (List<Map<String, String>>) resultMap.get("trans_result");
        Map<String,String> trans_resultMap = trans_result.get(0);
        return trans_resultMap.get("dst");
    }
}
